package net.freestu.sola.web.parser.Parse;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import net.freestu.sola.web.parser.Parse.Globle.GlobleVarOfParseSystem;
import net.freestu.sola.web.util.FileManager;
import net.freestu.sola.web.util.TimeOperator;



//未测试.

/**
 * 接受lib.cdut.edu.cn 文件夹(File)
 * 读取Link2hash文件,然后生成Delete Page 对象,
 * 然后写入到指定文件中.
 */
public class OutPutDeleteSite {
	
	
	private File sitedir;//www.lib.cdut.edu.cn
	
	private List dellist;
	
	public OutPutDeleteSite(File sitedir){
		
		this.sitedir=sitedir;
		dellist=new ArrayList();
	}
	
	/**
	 * 分析链接
	 */
	public void analyze(){
		
		File linkmd5=new File(sitedir,"OldLink2MD5");
		
		List urllist = new ArrayList();
		if (!linkmd5.exists()) {
			return;
		}
		try{
			FileReader fr = new FileReader(linkmd5);
			BufferedReader br = new BufferedReader(fr);
			String line;
			while (br.ready()) {
				line = br.readLine();// 读入了:http://www.sina.com\\vaveddve2dgve34vsv
				String[] str = line.split("\\\\", 2);// 这里应该有查异常。
				urllist.add(str[0]);//add:链接 http://www.
			}
			br.close();
			fr.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		
		//-----------------生成page对象-----------------------
		
		String time = TimeOperator.getCurrTime();
		Iterator iter=urllist.iterator();
		int i=1;
		while (iter.hasNext()){

			String url = (String) iter.next();
			String ii = String.valueOf(i);

			Page pp;
			pp = new Page(ii, "d", url, "d", "d", time, "d", "d", "d");
			dellist.add(pp);
			i++;
		}
	}
	
	/**
	 * 也按照一个xml文件最多装300个页面(这个还没有实现).！！！
	 */
	public void output(){
		
		//将文件里面的数据全部输出
		String name =sitedir.getName() + "@DELETE@";
		int n=1;
		while(dellist.size()!=0){
			
			List tmplist=sublist(dellist,300);//这里是每个XML最多装300个页面.
			FileManager.buildPageXml(GlobleVarOfParseSystem.UPDATE_FOLDER
					+ File.separator+"DELETE",name+n + ".xml");
			File xml = new File(GlobleVarOfParseSystem.UPDATE_FOLDER
					+ File.separator+"DELETE",name + n + ".xml");
			DataWriter de = new DataWriter(xml);
			
			if(tmplist==null){
				return;
			}
			de.writePages(tmplist);
			n++;
		}
	}
	
	/**
	 * 从dellist 中取出 num个元素放到List里面然后返回.()
	 * @param dellist
	 * @param num
	 * @return
	 */
	private List sublist(List dellist,int num){
		
		int total=dellist.size();
		
		if(total<num){//如果想取出的数目大于容器本身容量,则自动返回容量大小.
			num=total;
		}
		
		Iterator it=dellist.iterator();
		List result=new ArrayList();
		for(int i=0;i<num;i++){
			
		try {
				result.add(dellist.remove(0));
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}
		return result;
	}
	
	
	
	public static void main(String[] args){
		
		File f=new File("d://www.cdut.edu.cn");
		OutPutDeleteSite opds=new OutPutDeleteSite(f);
		
		opds.analyze();
		opds.output();
	}
}